//
//  YHLoginView.swift
//  SortProject
//
//  Created by Vast on 2022/7/23.
//

import UIKit

class YHLoginView: UIView {
    var nameTf: UITextField!
    var codeTf: UITextField!
    var codeBtn: UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    convenience init(frame: CGRect, status: Select_Status) {
        self.init(frame: frame)
        
        uiConfig(status: status)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func uiConfig(status: Select_Status) {
        let name = status == .Phone ? "手机号" : "账号"
        let namePlaceholder = status == .Phone ? "请输入手机号" : "请输入账号"
        let code = status == .Phone ? "验证码" : "密码"
        let codePlaceholder = status == .Phone ? "请输入验证码" : "请输入密码"

        let nameLb = createTitleLb(frame: CGRect(x: 24, y: 24, width: 50, height: 22), title: name)
        addSubview(nameLb)
        let nameView = createBackgroundView(frame: CGRect(x: 24, y: Int(nameLb.bottom) + 8, width: Int(self.width) - 48, height: 52))
        addSubview(nameView)
        nameTf = createTf(frame: CGRect(x: 16, y: 0, width: nameView.width - 32, height: 52), placeholder: namePlaceholder)
        nameView.addSubview(nameTf)
        let codeLb = createTitleLb(frame: CGRect(x: 24, y: 24 + nameView.bottom, width: 50, height: 22), title: code)
        addSubview(codeLb)
        let codeView = createBackgroundView(frame: CGRect(x: 24, y: codeLb.bottom + 8, width: nameView.width, height: 52))
        addSubview(codeView)
        if status == .Phone {
            let codeBtnWidth = 106
            codeTf = createTf(frame: CGRect(x: 16, y: 0, width: Int(codeView.width) - 32 - codeBtnWidth, height: 52), placeholder: codePlaceholder)
            codeView.addSubview(codeTf)
            codeBtn = UIButton.init(type: .custom)
            codeBtn?.frame = CGRect(x: Int(codeTf.right) + 16, y: 0, width: codeBtnWidth, height: 52)
            codeBtn?.setTitle("发送验证码", for: .normal)
            codeBtn?.titleLabel?.font = .systemFont(ofSize: 14)
            codeBtn?.setTitleColor(colorTheme, for: .normal)
            codeBtn?.addTarget(self, action: #selector(clickSendCode), for: .touchUpInside)
            codeView.addSubview(codeBtn!)
        } else {
            codeTf = createTf(frame: CGRect(x: 16, y: 0, width: codeView.width - 32, height: 52), placeholder: codePlaceholder)
            codeView.addSubview(codeTf)
        }
        
        
    }
    @objc func clickSendCode() {
        codeBtn?.isEnabled = false
        
        timeChange()
    }
    //    倒计时
    func timeChange() {
        var time = 60
        let codeTimer = DispatchSource.makeTimerSource(flags: .init(rawValue: 0), queue: DispatchQueue.global())
        codeTimer.schedule(deadline: .now(), repeating: .milliseconds(1000))
        codeTimer.setEventHandler {
            time = time - 1
            DispatchQueue.main.async {
                self.codeBtn?.isEnabled = false
            }
            if time < 0 {
                codeTimer.cancel()
                DispatchQueue.main.async {
                    self.codeBtn?.isEnabled = true
                    self.codeBtn?.setTitle("重新获取验证码", for: .normal)
                }
                return
            }
            DispatchQueue.main.async {
                self.codeBtn?.setTitle("\(time)秒后重新获取", for: .normal)
            }
        }
        codeTimer.activate()
    }
    func createBackgroundView(frame: CGRect) -> UIView {
        let view = UIView.init(frame: frame)
        view.backgroundColor = colorGray244
        view.layer.cornerRadius = 4
        view.layer.masksToBounds = true
        return view
    }
    func createTitleLb(frame: CGRect, title: String) -> UIView {
        let view = UILabel.init(frame: frame)
        view.backgroundColor = .white
        view.text = title
        view.textColor = .black.withAlphaComponent(0.5)
        view.font = .systemFont(ofSize: 16)
        return view
    }
    
    func createTf(frame: CGRect, placeholder: String) -> UITextField {
        let tf = UITextField.init(frame: frame)
        tf.clearButtonMode = .whileEditing
        tf.placeholder = placeholder
        tf.font = .systemFont(ofSize: 14, weight: .medium)
        
        return tf
    }
    
    func createCodeButton() {
        
    }
}
